package com.lanou.beautifulwardrobe.activity_zhuti.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.IntroOutfit;

import java.util.List;

/**
 * Created by dllo on 15/11/6.
 */
public class IntroTListViewAdapter extends BaseAdapter {
    private List<IntroOutfit> datas;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private ImageLoader imageLoader;

    public void addData(List<IntroOutfit> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas != null && datas.size() > 0 ? datas.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_introthree_item, null);
            holder = new ViewHolder();
            holder.introTImg = (NetworkImageView) convertView.findViewById(R.id.introTImg);
            holder.introTSecLvt = (ListView) convertView.findViewById(R.id.introTSecLvt);
            holder.adapter = new IntroTSecListViewAdapter();
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (datas != null && datas.size() > 0) {
            getBitMap(datas.get(position).getPic_url(), holder.introTImg);
            holder.introTSecLvt.setAdapter(holder.adapter);
            holder.adapter.addData(datas.get(position).getCategories());


            int totalHeight = 0;
            for (int i = 0; i < holder.adapter.getCount(); i++) {
                View listItem = holder.adapter.getView(i, null, holder.introTSecLvt);
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }

            ViewGroup.LayoutParams params = holder.introTSecLvt.getLayoutParams();
            params.height = totalHeight + (holder.introTSecLvt.getDividerHeight() * (holder.adapter.getCount() - 1));
            ((ViewGroup.MarginLayoutParams) params).setMargins(10, 10, 10, 10);
            holder.introTSecLvt.setLayoutParams(params);
        }
        return convertView;
    }

    class ViewHolder {
        private NetworkImageView introTImg;
        private ListView introTSecLvt;
        private IntroTSecListViewAdapter adapter;

    }

    /** 拉取图片 **/
    private void getBitMap(String url, NetworkImageView imageView){
        imageLoader = volleySingleton.getImageLoader();
        imageView.setDefaultImageResId(R.mipmap.ic_launcher);
        imageView.setErrorImageResId(R.mipmap.ic_launcher);
        imageView.setImageUrl(url, imageLoader);
    }
}
